package com.xjh.him.adapter.emotion.classic;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.xjh.him.utils.EmotionUtils;
import com.xjh.him.utils.XToastUtils;

import java.util.List;

public class ClassicEmotionAdapter extends BaseAdapter {
    private Context context;
    private List<String> emotionNames;
    private int itemWidth;

    public ClassicEmotionAdapter(Context context, List<String> emotionNames, int itemWidth) {
        this.context = context;
        this.emotionNames = emotionNames;
        this.itemWidth = itemWidth;
    }

    @Override
    public int getCount() {
        return emotionNames.size();
    }

    @Override
    public String getItem(int position) {
        return emotionNames.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView iv_emotion = new ImageView(context);
        // 设置内边距
        iv_emotion.setPadding(itemWidth / 8, itemWidth / 8, itemWidth / 8, itemWidth / 8);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(itemWidth, itemWidth);
        iv_emotion.setLayoutParams(params);
        // 设置表情图片
        String emotionName = String.valueOf(emotionNames.get(position));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int resId = EmotionUtils.getEmojiByKey(EmotionUtils.EMOTION_CLASSIC_TYPE, emotionName);
            iv_emotion.setImageResource(resId);
        }else {
            XToastUtils.toast("build sdk version must greater 19, current is " + Build.VERSION.SDK_INT);
        }
        return iv_emotion;
    }
}
